package com.ruoyi.web.controller.module.order;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.employee.domain.EmployeeUser;
import com.ruoyi.employee.service.IEmployeeUserService;
import com.ruoyi.market.domain.ConfigEmployee;
import com.ruoyi.market.service.IConfigEmployeeService;
import com.ruoyi.order.domain.OrderCashEmployee;
import com.ruoyi.order.service.IOrderCashEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 店员提现订单信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/order/cash/employee/")
public class OrderCashEmployeeController extends BaseController
{
    @Autowired
    private IOrderCashEmployeeService orderCashEmployeeService;

    @Autowired
    private IEmployeeUserService employeeUserService;

    /**
     * 获取店员提现订单列表
     */
    @PostMapping("/orderCashEmployeeList")
    public TableDataInfo list(@RequestBody OrderCashEmployee orderCashEmployee, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            orderCashEmployee.setShopId(shopId);
        }
        startPageByPost();
        List<OrderCashEmployee> list = orderCashEmployeeService.selectOrderCashEmployeeList(orderCashEmployee);
        return getDataTable(list);
    }

    @Log(title = "店员提现订单管理 - 导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('order:cash:employee:export')")
    @PostMapping("/orderCashEmployeeExport")
    public void export(HttpServletResponse response, OrderCashEmployee orderCashEmployee)
    {
        List<OrderCashEmployee> list = orderCashEmployeeService.selectOrderCashEmployeeList(orderCashEmployee);
        ExcelUtil<OrderCashEmployee> util = new ExcelUtil<OrderCashEmployee>(OrderCashEmployee.class);
        util.exportExcel(response, list, "店员提现订单数据");
    }

    /**
     * 根据店员提现订单编号获取详细信息
     */
    @GetMapping(value = { "/", "/{orderNo}" })
    public AjaxResult getInfo(@PathVariable(value = "orderNo", required = false) String orderNo)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(orderNo))
        {
            OrderCashEmployee orderCashEmployee = orderCashEmployeeService.selectOrderCashEmployeeByOrderNo(orderNo);
            ajax.put(AjaxResult.DATA_TAG, orderCashEmployee);
        }
        return ajax;
    }

    /**
     * 新增店员提现订单
     */
    @PostMapping("/orderCashEmployeeCreate")
    public AjaxResult orderCashEmployeeCreate(@Validated @RequestBody OrderCashEmployee orderCashEmployee, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            orderCashEmployee.setShopId(shopId);
        }
        // 当前登录用户
        Long userId = SecurityUtils.getUserId();
        EmployeeUser user = employeeUserService.selectUserById(userId);
        employeeUserService.checkUserAllowed(user);
        return toAjax(orderCashEmployeeService.insertOrderCashEmployee(orderCashEmployee));
    }

    /**
     * 修改店员提现订单
     */
    @PreAuthorize("@ss.hasPermi('order:cash:employee:edit')")
    @PostMapping("/orderCashEmployeeEdit")
    public AjaxResult orderCashEmployeeEdit(@Validated @RequestBody OrderCashEmployee orderCashEmployee)
    {
        return toAjax(orderCashEmployeeService.updateOrderCashEmployee(orderCashEmployee));
    }
}
